﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;
using System.Threading;

public partial class admin_AfficheInformSetting_AddAfficheInform : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

        }
    }

    protected void upload()
    {
        string rootPath = ConfigurationManager.AppSettings["AfficheInformAffixFileRoot"];
        string fileName = this.AffixFile.FileName;
        string prefix = DateTime.Now.Ticks.ToString();
        AffixFile.SaveAs(rootPath + "\\" + prefix + "_" + fileName);
    }

    protected void BtnUpload_Click(object sender, EventArgs e)
    {
        if (this.AffixFile.HasFile)
        {
            string rootPath = ConfigurationManager.AppSettings["AfficheInformAffixFileRoot"];
            DirectoryInfo di = new DirectoryInfo(rootPath);
            if (!di.Exists)
            {
                di.Create();
            }
            string fileName = this.AffixFile.FileName;
            string prefix = DateTime.Now.Ticks.ToString();
            Thread t = new Thread(new ThreadStart(upload));
            t.Start();
            this.ListBox1.Items.Add(new ListItem(fileName, prefix + "_" + fileName));
        }
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        string sids = "," + Request.Form.Get("HidSids");
        if (string.IsNullOrEmpty(sids.Trim()))
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送范围不能为空')", true);
            return;
        }
        string aiTitle = Request.Form.Get(this.TxtTitle.ClientID);
        if (string.IsNullOrEmpty(aiTitle.Trim()))
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('标题不能为空')", true);
            return;
        }
        DateTime start = DateTime.Parse(Request.Form.Get(this.TxtStart.ClientID));
        if (string.IsNullOrEmpty(start.ToString().Trim()))
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('创建时间不能为空')", true);
            return;
        }
        DateTime end = DateTime.Parse(Request.Form.Get(this.TxtEnd.ClientID));

        if (string.IsNullOrEmpty(end.ToString().Trim()))
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('终止时间时间不能为空')", true);
            return;
        }
        string aiContent = this.MyEditor.value;
        if (string.IsNullOrEmpty(aiContent.Trim()))
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('内容不能为空')", true);
            return;
        }

        string[] affixFiles = Request.Form.GetValues(this.ListBox1.ClientID);
        string affixFilePaths = "";
        if (affixFiles != null && affixFiles.Length > 0)
        {
            for (int i = 0; i < affixFiles.Length; i++)
            {
                if (i == affixFiles.Length - 1)
                {
                    affixFilePaths += affixFiles[i];
                }
                else
                {
                    affixFilePaths += (affixFiles[i] + ",");
                }
            }
        }
        User curLoginUser = (User)Session["user"];
        if (!"".Equals(aiTitle.Trim()) && !"".Equals(aiContent.Trim()) && !"".Equals(Request.Form.Get(this.TxtStart.ClientID)) && !"".Equals(Request.Form.Get(this.TxtEnd.ClientID)))
        {
            AfficheInformtbService aiService = new AfficheInformtbService();
            if (aiService.saveAfficheInformInfo(aiTitle, aiContent, DateTime.Now, start, end, affixFilePaths, sids, curLoginUser.UserId)[0] == 1)
            {
                Response.Redirect("Index.aspx");
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert(内容填写不合理！);", true);
        }
    }

    protected void BtnDelFile_Click(object sender, EventArgs e)
    {
        string[] selectFiles = Request.Form.GetValues(this.ListBox1.ClientID);
        if (selectFiles != null)
        {
            for (int i = 0; i < selectFiles.Length; i++)
            {
                string filepath = ConfigurationManager.AppSettings["AfficheInformAffixFileRoot"] + "\\" + selectFiles[i];
                FileInfo fi = new FileInfo(filepath);
                if (fi.Exists)
                {
                    fi.Delete();
                    for (int j = 0; j < this.ListBox1.Items.Count; j++)
                    {
                        if (ListBox1.Items[j].Value.Equals(selectFiles[i]))
                        {
                            ListBox1.Items.Remove(ListBox1.Items[j]);
                            j--;
                        }
                    }
                }
            }
        }
    }
}
